#-*-coding:utf-8 -*
import tushare as ts
ts.set_token("6667cd4a2326f2f937062a0f4fb59aea5c56d13b1f6f26225f115fe9")
pro = ts.pro_api()

import time 
start = time.time()


import pandas as pd
data_1 = pd.DataFrame()
data_1 = pro.stock_basic(exchange='', list_status='L', market="主板",fields='ts_code,name,area,industry,list_date')

import pymysql

from sqlalchemy import create_engine
DB_STRING = 'mysql+pymysql://root:kirin@localhost:3306/stock?charset=utf8'
engine = create_engine(DB_STRING)


ts_market=pd.DataFrame()
for i in data_1['ts_code'][:50]:
    try:
        ts_market=ts_market.append(pro.stock_basic(ts_code=i,fields="symbol,market,name,industry,area,list_date"), ignore_index=True)
    except:
        pass

# cur.execute('SELECT * FROM ts_market')
# res= cur.fetchall()
ts_market.to_sql("ts_market_45",engine,if_exists="append",index=True)
print("执行时间 {} 秒".format(round(time.time() - start, 2)))


sql = ''' select * from ts_market_45; '''
# read_sql_query的两个参数: sql语句， 数据库连接
df = pd.read_sql_query(sql, engine)
# 输出employee表的查询结果
print(df)




